package com.example.lxj.system.controller;

import com.example.lxj.base.annoation.Permission;
import com.example.lxj.base.controller.BaseController;
import com.example.lxj.base.entity.AjaxRes;
import com.example.lxj.base.entity.PageDTO;
import com.example.lxj.base.entity.TableDataInfo;
import com.example.lxj.system.entity.SysRole;
import com.example.lxj.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
 * 角色信息Controller
 *
 * @author ruoyi
 * @date 2024-10-11
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询角色信息列表
     */
    @Permission(value ="system:role:list")
    @GetMapping("/list")
    public TableDataInfo list(SysRole sysRole, PageDTO pageDTO)
    {
        startPage(Optional.ofNullable(pageDTO.getPageNo()).orElse(1), Optional.ofNullable(pageDTO.getPageSize()).orElse(10));
        List<SysRole> list = sysRoleService.selectSysRoleList(sysRole);
        return getDataTable(list);
    }

    /**
     * 获取所有角色列表
     * @return
     */
    @GetMapping("/allRoleList")
    public AjaxRes allRoleList(){
        return AjaxRes.success(sysRoleService.selectSysRoleList(null));
    }

    /**
     * 获取角色信息详细信息
     */
    @Permission("system:role:query")
    @GetMapping(value = "/{roleId}")
    public AjaxRes getInfo(@PathVariable("roleId") Long roleId)
    {
        return success(sysRoleService.selectSysRoleByRoleId(roleId));
    }

    /**
     * 新增角色信息
     */
    @Permission("system:role:add")
    @PostMapping("/add")
    public AjaxRes add(@RequestBody SysRole sysRole)
    {
        return toAjax(sysRoleService.insertSysRole(sysRole));
    }

    /**
     * 修改角色信息
     */
    @Permission("system:role:edit")
    @PutMapping
    public AjaxRes edit(@RequestBody SysRole sysRole)
    {
        return toAjax(sysRoleService.updateSysRole(sysRole));
    }

    /**
     * 删除角色信息
     */
    @Permission("system:role:remove")
    @DeleteMapping("/{roleId}")
    public AjaxRes remove(@PathVariable("roleId") Long roleId)
    {
        System.out.println("role"+roleId);
        return toAjax(sysRoleService.deleteSysRoleByRoleId(roleId));
    }
}
